#!/bin/sh

# Determine the path to this application.
CURDIR="`pwd`"
APPDIR=`dirname "$0"`
cd "${APPDIR}"
APPDIR="`pwd`"
cd "${CURDIR}"

# setup working space
[ ! -d $HOME/.config/wallpaper ] && mkdir $HOME/.config/wallpaper
PREFS="$HOME/.config/wallpaper/preferences"
[ ! -f $PREFS ] && touch $PREFS
. $PREFS
[ "$INT" = "" ] && echo 'INT="15"' >> $PREFS
[ "$SLIDEDIR" = "" ] && echo 'SLIDEDIR="/usr/share/backgrounds"' >> $PREFS
[ "$FILER" = "" ] && echo 'FILER="defaultfilemanager"' >> $PREFS
[ "$VIEWER" = "" ] && echo 'VIEWER="defaultimageviewer"' >> $PREFS
[ "$IMGEDITOR" = "" ] && echo 'IMGEDITOR="defaultimageeditor"' >> $PREFS
. $PREFS

# Get translations
ENVIRONMENT_LANG=`echo $LANG | sed "s/\(..\).*/\1/"` #detect environment language 
case $ENVIRONMENT_LANG in
	de)	export LANGUAGE="german" ;;
	en)	export LANGUAGE="english" ;;
	es)	export LANGUAGE="spanish" ;;
	fr)	export LANGUAGE="french" ;;
	it)	export LANGUAGE="italian" ;;
	nb)	export LANGUAGE="norwegian" ;; #translation is norwegian bokmål
	nl)	export LANGUAGE="dutch" ;;
	nn)	export LANGUAGE="norwegian" ;; #Use norwegian bokmål, since there is no norwegian nynorsk
	ru)	export LANGUAGE="russian" ;;
	*)	export LANGUAGE="english" ;; #english for contries without translation
esac
. $APPDIR/locale/english.po #always run to fill gaps in translation
[ -f "$APPDIR/locale/${LANGUAGE}.po" ] && . $APPDIR/locale/${LANGUAGE}.po 2> /dev/null

# what is the old backround?
OLDIMAGE="`cat $HOME/.config/rox.sourceforge.net/ROX-Filer/pb_default | grep '<backdrop ' | cut -f 2 -d '>' | cut -f 1 -d '<'`"
OLDMODE="`cat $HOME/.config/rox.sourceforge.net/ROX-Filer/pb_default | grep '<backdrop ' | cut -f 2 -d '"'`" #'
if [ "$OLDMODE" = "" ];then
 OLDMODE="Scale"
fi
[ "$OLDMODE" = "Stretched" ] && OLDMODE="Stretch"
[ "$OLDMODE" = "Scaled" ] && OLDMODE="Scale"
[ "$OLDMODE" = "Tiled" ] && OLDMODE="Tile"
[ "$OLDMODE" = "Centred" ] && OLDMODE="Centre"
echo $OLDMODE > $HOME/.config/wallpaper/backgroundmode

# if run with an image file as an argument
if [ "$1" = "-play" ];then
 [ -f $HOME/.config/sessionrc ] && . $HOME/.config/sessionrc
 [ -f $HOME/.config/wallpaper/preferences ] && . $HOME/.config/wallpaper/preferences
 [ "$SLIDEDIR" = "" ] && SLIDEDIR="/usr/share/backgrounds"
 exec $APPDIR/slideshow -start $SLIDEDIR/file
fi
[ "$1" = "-about" ] && exec $APPDIR/functions -about
if [ ! "$1" = "" ];then
 exec $APPDIR/set_bg $1
fi

# Radio buttons
CENTREBUTTON="<radiobutton>
<label>$LOC_CENTRE</label>
<action>echo Centre > $HOME/.config/wallpaper/backgroundmode</action>
<action>$APPDIR/slideshow -stop</action>
<action>$APPDIR/set_bg</action>
<action type=\"refresh\">TEXT</action>
</radiobutton>"
TILEBUTTON="<radiobutton>
<label>$LOC_TILE</label>
<action>echo Tile > $HOME/.config/wallpaper/backgroundmode</action>
<action>$APPDIR/slideshow -stop</action>
<action>$APPDIR/set_bg</action>
<action type=\"refresh\">TEXT</action>
</radiobutton>"
SCALEBUTTON="<radiobutton>
<label>$LOC_SCALE</label>
<action>echo Scale > $HOME/.config/wallpaper/backgroundmode</action>
<action>$APPDIR/slideshow -stop</action>
<action>$APPDIR/set_bg</action>
<action type=\"refresh\">TEXT</action>
</radiobutton>"
STRETCHBUTTON="<radiobutton>
<label>$LOC_STRETCH</label>
<action>echo Stretch > $HOME/.config/wallpaper/backgroundmode</action>
<action>$APPDIR/slideshow -stop</action>
<action>$APPDIR/set_bg</action>
<action type=\"refresh\">TEXT</action>
</radiobutton>"

case $OLDMODE in
 Centre)
 RADIOBUTTONS="$CENTREBUTTON
 $TILEBUTTON
 $SCALEBUTTON
 $STRETCHBUTTON"
 ;;
 Tile)
 RADIOBUTTONS="$TILEBUTTON
 $CENTREBUTTON
 $SCALEBUTTON
 $STRETCHBUTTON"
 ;;
 Scale)
 RADIOBUTTONS="$SCALEBUTTON
 $CENTREBUTTON
 $TILEBUTTON
 $STRETCHBUTTON"
 ;;
 Stretch)
 RADIOBUTTONS="$STRETCHBUTTON
 $CENTREBUTTON
 $TILEBUTTON
 $SCALEBUTTON"
 ;;
esac
if [ "$OLDIMAGE" = "" ];then
 OLDIMAGE="[none]"
 DIR="/usr/share/backgrounds"
else
 DIR="`dirname $OLDIMAGE`"
fi
echo "$OLDIMAGE" > $HOME/.config/wallpaper/bg_img
PREVDIR="`pwd`"

# Reset mode
echo $OLDMODE > $HOME/.config/wallpaper/backgroundmode

# setup menu entries for various graphics programs
if [ ! "`which gimp`" = "" ];then
 GIMP='<menuitem icon="gimp"><label>Gimp</label><action>gimp $IMAGE &</action></menuitem>'
fi
if [ ! "`which cinepaint`" = "" ];then
 CINEPAINT='<menuitem icon="cinepaint"><label>Cinepaint</label><action>cinepaint $IMAGE &</action></menuitem>'
fi
if [ ! "`which inkscape`" = "" ];then
 INKSCAPE='<menuitem icon="inkscape"><label>Inkscape</label><action>inkscape $IMAGE &</action></menuitem>'
fi
if [ ! "`which inkscapelite`" = "" ];then
 INKSCAPELITE='<menuitem icon="inkscape"><label>Inkscapelite</label><action>inkscapelite $IMAGE &</action></menuitem>'
fi
if [ ! "`which krita`" = "" ];then
 KRITA='<menuitem icon="krita"><label>Krita</label><action>krita $IMAGE &</action></menuitem>'
fi
if [ ! "`which xnview`" = "" ];then
 XNVIEW='<menuitem icon="xnview"><label>XnView</label><action>xnview -browse $IMAGE &</action></menuitem>'
fi
if [ ! "`which gqview`" = "" ];then
 GQVIEW='<menuitem icon="gqview"><label>Gqview</label><action>gqview $IMAGE &</action></menuitem>'
fi
if [ ! "`which gpicview`" = "" ];then
 GPICVIEW='<menuitem icon="gpicview"><label>Gpicview</label><action>gpicview $IMAGE &</action></menuitem>'
fi
if [ ! "`which fotox`" = "" ];then
 FOTOX='<menuitem icon="fotox"><label>Fotox</label><action>fotox $IMAGE &</action></menuitem>'
fi

cd "$DIR"

export BG_DLG="
<window title=\"$LOC_MAIN\" icon_name=\"preferences-desktop-wallpaper\">
 <vbox>
  <menubar>
   <menu>
    <menuitem icon=\"multimedia-player\">
     <label>$LOC_SLIDESHOW</label>
     <action>$APPDIR/slideshow -start \$IMAGE &</action>
    </menuitem>
    <menuitem icon=\"gtk-preferences\">
     <label>$LOC_OPTIONS</label>
     <action>$APPDIR/functions -preferences &</action>
    </menuitem>
    <separator></separator>
    <menuitem icon=\"gtk-quit\">
     <label>$LOC_QUIT</label>
     <action>EXIT:abort</action>
    </menuitem>
    <label>$LOC_FILE</label>
   </menu>
   <menu>
    <menuitem icon=\"applications-graphics\">
     <label>mtPaint</label>
     <action>mtpaint \$IMAGE &</action>
    </menuitem>
    <separator></separator>
    $GIMP
    $CINEPAINT
    $INKSCAPE
    $INKSCAPELITE
    $KRITA
    <label>$LOC_EDIT</label>
   </menu>
   <menu>
    $GPICVIEW
    $FOTOX
    <separator></separator>
    $XNVIEW
    $GQVIEW
    <label>$LOC_VIEW</label>
   </menu>
   <menu>
    <menuitem stock=\"gtk-about\">
     <action>$APPDIR/functions -about</action>
    </menuitem>
    <label>$LOC_HELP</label>
   </menu>
  </menubar>
  <hbox>
   <vbox>
    <frame $LOC_FILE_FRAME>
     <chooser>
      <height>300</height><width>550</width>
      <variable>IMAGE</variable>
      <action>$APPDIR/slideshow -stop</action>
      <action>$APPDIR/set_bg \"\$IMAGE\"</action>
      <action>echo \"\$IMAGE\" > \$HOME/.config/wallpaper/bg_img</action>
      <action type=\"refresh\">TEXT</action>
     </chooser>
     <frame $LOC_CURRENT_IMAGE>
      <text>
       <input>cat $HOME/.config/wallpaper/bg_img</input>
       <variable>TEXT</variable>
      </text>
     </frame>
    </frame>
   </vbox>
   <vbox>
    <frame $LOC_MODE>
     $RADIOBUTTONS
    </frame>
    <frame $LOC_ACTIONS>
     <button>
      <label>$LOC_APPLY</label>
      <input file stock=\"gtk-apply\"></input>
      <action>$APPDIR/slideshow -stop</action>
      <action>$APPDIR/set_bg \"\$IMAGE\"</action>
      <action>echo \"\$IMAGE\" > \$HOME/.config/wallpaper/bg_img</action>
      <action type=\"refresh\">TEXT</action>
     </button>
     <button>
      <label>$LOC_CLEAR</label>
      <input file stock=\"gtk-clear\"></input>
      <action>$APPDIR/slideshow -stop</action>
      <action>$APPDIR/set_bg -clear</action>
      <action type=\"refresh\">TEXT</action>
     </button>
     <button>
      <label>$LOC_EDIT</label>
      <input file icon=\"applications-graphics\"></input>
      <action>$IMGEDITOR \$IMAGE &</action>
     </button>
     <button>
      <label>$LOC_VIEW</label>
      <input file icon=\"image-x-generic\"></input>
      <action>$VIEWER \$IMAGE &</action>
     </button>
     <button>
      <label>$LOC_FILER</label>
      <input file stock=\"gtk-directory\"></input>
      <action>dirname \$IMAGE > \$HOME/.config/wallpaper/bgdir</action>
      <action>$APPDIR/functions -filer</action>
     </button>
     <button>
      <input file stock=\"gtk-close\"></input>
      <label>$LOC_CLOSE</label>
     </button>
    </frame>
   </vbox>
  </hbox>
 </vbox>
</window>
"
gtkdialog3 --program BG_DLG

cd "$PREVDIR"












